package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdSensitive;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 谢金成
 * @date 2024/4/27 19:26
 */
@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /**
     * 敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        // 1.检查参数
        dto.checkParam();
        // 2.分页查询
        IPage<WmSensitive> page = new Page(dto.getPage(), dto.getSize());
        page = page(page, Wrappers.<WmSensitive>lambdaQuery()
                .like(StringUtils.isNoneBlank(dto.getName()), WmSensitive::getSensitives, dto.getName())
                .orderByDesc(WmSensitive::getCreatedTime));

        // 3.结果封装
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        // 4.返回结果

        return pageResponseResult;
    }

    /**
     * 添加敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        // 1.校验参数
        if (StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2.查询敏感词数据
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, adSensitive.getSensitives()));
        // 3.数据不存在
        if (sensitive == null) {
            // 4.添加敏感词
            sensitive = new WmSensitive();
            sensitive.setSensitives(adSensitive.getSensitives());
            sensitive.setCreatedTime(new Date());

            save(sensitive);
        }
        // 3.结果返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        // 1.参数校验
        if (StringUtils.isBlank(adSensitive.getSensitives()) || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.查询数据
        WmSensitive sensitive = getOne(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getId, adSensitive.getId()));

        // 3.数据不存在
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 4.修改数据
        sensitive.setSensitives(adSensitive.getSensitives());
        sensitive.setCreatedTime(new Date());

        boolean result = updateById(sensitive);
        if (!result) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "修改敏感词失败");
        }

        // 5.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        // 1.参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2.删除数据
        boolean result = removeById(id);
        // 3.删除失败
        if (!result) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "删除敏感词失败");
        }
        // 4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
